﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckSitNum : MonoBehaviour
{
    // Start is called before the first frame update
    public int count;
    public List<GameObject> Players;
    bool checkFlag = false;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(checkFlag)
        {
            CheckPlayersLeaveBench();
        }
     
    }


    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            checkFlag = true;
            Players.Add(collision.gameObject);
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            checkFlag = true;
            Players.Remove(collision.gameObject);
        }
    }


    void CheckPlayersLeaveBench()
    {
        for(int i=Players.Count-1;i>0;i--)
        {
            //if ((Mathf.Abs(transform.position.x - Players[i].transform.position.x) <= 12.5f) && Mathf.Abs(transform.position.z - Players[i].transform.position.z) <= 20.7f)
            if(Players[i].GetComponent<AnimateCtrl>().isSitting)
            {

            }
            else
            {
                Players[i].GetComponent<BubbleBlending>().sitFlag = false;
                //Players.Remove(Players[i]);

                if(CountOfPlayers() == 0)
                {
                    checkFlag = false;
                }
            }
        }
        count = CountOfPlayers();
    }
    public int CountOfPlayers()
    {
        int realCount = 0;
        for(int i=Players.Count-1;i>=0;i--)
        {
            if(Players[i]!=null)
            {
                realCount++;
            }
        }
        return realCount;
    }
}
